package GUI; //done

import java.awt.Rectangle;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Client.ConnectionToServer;

/**
 * This Class controls Type Management Window. 
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public abstract class TypeManagementWindow extends JPanel{
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;
	
	/** The Categories ID ComboBox. */
	private JComboBox categoriesIDComboBox = null;
	
	/** The Types ID ComboBox. */
	private JComboBox typesIDComboBox = null;
	
	/** The Products ID Text Area. */
	private JTextArea productsIDTextArea = null;
	
	/** The Categories ID Label. */
	private JLabel categoriesIDLabel = null;
	
	/** The Types ID Label. */
	private JLabel typesIDLabel = null;
	
	/** The Products ID Label. */
	private JLabel productsIDLabel = null;
	
	/** The Add Button Label. */
	private JLabel addbuttonLabel = null;
	
	/** The ID Label. */
	private JLabel idLabel = null;
	
	/** The Name Label. */
	private JLabel nameLabel = null;
	
	/** The ID Text Field. */
	private JTextField idTextField = null;
	
	/** The Name Text Field. */
	private JTextField nameTextField = null;
	
	/** The Save Button. */
	private JButton saveButton = null;
	
	/** The Cancel Button. */
	private JButton cancelButton = null;
	
	/** The Update Button. */
	private JButton updateButton = null;
	
	/** The Remove Button. */
	private JButton removeButton = null;
	
	/** The Products ID TextArea ScrollPane. */
	private JScrollPane productsIDTextAreaScrollPane = null;
	
	/** The Category ID. */
	private int CategoryID = 0;
	
	/**
	 * Default constructor
	 */
	public TypeManagementWindow(){
		super();
		initialize();	
	}
	
	/**
	 * This function initializes TypeManagementWindow
	 */
	private void initialize() {
		categoriesIDLabel = new JLabel();
		categoriesIDLabel.setBounds(new Rectangle(15, 20, 130, 22));
		categoriesIDLabel.setText("All sections:");
		
		typesIDLabel = new JLabel();
		typesIDLabel.setBounds(new Rectangle(150, 20, 130, 22));
		typesIDLabel.setText("All types:");
		
		productsIDLabel = new JLabel();
		productsIDLabel.setBounds(new Rectangle(290, 20, 130, 22));
		productsIDLabel.setText("All type products:");
		
		addbuttonLabel = new JLabel();
		addbuttonLabel.setBounds(new Rectangle(15, 80, 300, 22));
		addbuttonLabel.setText("PLZ ***:");
		
		idLabel = new JLabel();
		idLabel.setBounds(new Rectangle(15, 100, 130, 22));
		idLabel.setText("ID ***:");
		
		nameLabel = new JLabel();
		nameLabel.setBounds(new Rectangle(15, 130, 130, 22));
		nameLabel.setText("NAME ***:");
		
		this.setSize(450, 250);
		this.setLayout(null);
		this.add(categoriesIDLabel, null);
		this.add(typesIDLabel, null);
		this.add(productsIDLabel, null);
		this.add(addbuttonLabel, null);
		this.add(idLabel, null);
		this.add(nameLabel, null);
		this.add(getIdTextField(), null);
		this.add(getNameTextField(), null);
		this.add(getSaveButton(), null);
		this.add(getUpdateButton(), null);
		this.add(getCancelButton(),null);
		this.add(getRemoveButton(),null);
		this.add(getCategoryIDComboBox(),null);
		this.add(getTypesIDComboBox(),null);
		this.add(getProductsIDTextAreaScrollPane(), null);		
	}
	
	/**
	 * @return the sectionsIDLabel
	 */
	public JLabel getSectionsIDLabel() {
		return categoriesIDLabel;
	}

	/**
	 * @return the addbuttonLabel
	 */
	public JLabel getAddbuttonLabel() {
		return addbuttonLabel;
	}

	/**
	 * @return the typesIDLabel
	 */
	public JLabel getTypesIDLabel() {
		return typesIDLabel;
	}

	/**
	 * @return the productsIDLabel
	 */
	public JLabel getProductsIDLabel() {
		return productsIDLabel;
	}

	/**
	 * @return the idLabel
	 */
	public JLabel getIdLabel() {
		return idLabel;
	}

	/**
	 * @return the nameLabel
	 */
	public JLabel getNameLabel() {
		return nameLabel;
	}

	/**
	 * This function initializes IdTextField	
	 */
	public JTextField getIdTextField(){
		if (idTextField == null) {
			idTextField = new JTextField();
			idTextField.setBounds(new Rectangle(150, 100, 127, 25));
		}
		return idTextField;
	}
	
	/**
	 * This function initializes NameTextField	
	 */
	public JTextField getNameTextField(){
		if (nameTextField == null) {
			nameTextField = new JTextField();
			nameTextField.setBounds(new Rectangle(150, 130, 127, 25));
		}
		return nameTextField;
	}
	
	/**
	 * This function initializes CancelButton
	 */	
	public JButton getCancelButton(){
		if (cancelButton == null){
			cancelButton = new JButton();
			cancelButton.setText("Back");
			cancelButton.setBounds(new Rectangle(15, 170, 90, 29));
		}	
		return cancelButton;				
	}
	
	/**
	 * This function initializes UpdateButton
	 */	
	public JButton getUpdateButton(){
		if (updateButton == null){
			updateButton = new JButton();
			updateButton.setText("UPDATE");
			updateButton.setBounds(new Rectangle(110, 170, 90, 29));
		}	
		return updateButton;				
	}
	
	/**
	 * This function initializes RemoveButton
	 */	
	public JButton getRemoveButton(){
		if (removeButton == null){
			removeButton = new JButton();
			removeButton.setText("Remove");
			removeButton.setBounds(new Rectangle(205, 170, 90, 29));
		}	
		return removeButton;				
	}
	
	/**
	 * This function initializes SaveButton
	 */	
	public JButton getSaveButton(){
		if (saveButton == null){
			saveButton = new JButton();
			saveButton.setText("Add new");
			saveButton.setBounds(new Rectangle(300, 170, 90, 29));
		}	
		return saveButton;				
	}
	
	/**
	 * This function initializes CategoryIDComboBox	
	 */
	public JComboBox getCategoryIDComboBox(){
		if (categoriesIDComboBox == null) {
			categoriesIDComboBox = new JComboBox();
			categoriesIDComboBox.setBounds(new Rectangle(15, 40, 127, 25));
			updateCategoriesIDComboBox();			
		}
		return categoriesIDComboBox;
	}
	
	/**
	 * This function loads all categories from DB to updateCategoriesIDComboBox
	 */
	public void updateCategoriesIDComboBox(){
		String[] sectIDList = null;
		int i=0,itemcount = categoriesIDComboBox.getItemCount();
		if (itemcount > 0) {
			categoriesIDComboBox.addItem(":");
			categoriesIDComboBox.setSelectedItem(":");				
		}
		for (i=0; i < itemcount ; i++ ){				
			categoriesIDComboBox.removeItemAt(0);						
		}			
		try{
			ConnectionToServer connectiontoserver = new ConnectionToServer();		
			sectIDList = (String[]) connectiontoserver.loadAllCategories();
			if (sectIDList == null){
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				categoriesIDComboBox.addItem(":");
			}	
			else{
				int len = sectIDList.length;
				if (len == 0)
					categoriesIDComboBox.addItem(":");						
				for(i=0; i < len; i++){
					categoriesIDComboBox.addItem(sectIDList[i]);
				}	
			}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "ERROR!!!");
			categoriesIDComboBox.addItem(":");
		}
		categoriesIDComboBox.addActionListener(new java.awt.event.ActionListener(){
			public void actionPerformed(java.awt.event.ActionEvent e){
				try{
					String CatID = (String)categoriesIDComboBox.getSelectedItem();
					if(!(CatID == ":")){
					CategoryID = Integer.parseInt(CatID.substring(0, CatID.indexOf(":")));
					updateTypesIDComboBox();
					typesIDComboBox.setVisible(true);
					}
				}
				catch(Exception exc){
					JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
					return;
				}
			}
		});
	}
	
	/**
	 * This function initializes TypesIDComboBox	
	 */
	public JComboBox getTypesIDComboBox(){
		if (typesIDComboBox == null) {
			typesIDComboBox = new JComboBox();
			typesIDComboBox.setBounds(new Rectangle(150, 40, 127, 25));
			typesIDComboBox.setVisible(false);
		}
		return typesIDComboBox;
	}
	
	/**
	 * This function initializes ProductsIDTextArea	
	 */
	public JTextArea getProductsIDTextArea(){
		if (productsIDTextArea == null){
			productsIDTextArea = new JTextArea();
			productsIDTextArea.setBounds(new Rectangle(290, 40, 130, 80));	
			productsIDTextArea.setEditable(false);
		}
		return productsIDTextArea;
	}
	
	/**
	 * This function initializes ProductsIDTextAreaScrollPane
	 */
	public JScrollPane getProductsIDTextAreaScrollPane(){
		if (productsIDTextAreaScrollPane == null){
			productsIDTextAreaScrollPane = new JScrollPane(getProductsIDTextArea());
			AdjustmentListener listener = new AdjustmentListener(){
				public void adjustmentValueChanged(AdjustmentEvent evt){
				}	
			};
			productsIDTextAreaScrollPane.getHorizontalScrollBar().addAdjustmentListener(listener);
			productsIDTextAreaScrollPane.getVerticalScrollBar().addAdjustmentListener(listener);
			productsIDTextAreaScrollPane.setBounds(new Rectangle(290, 40, 140, 90));			
		}
		return productsIDTextAreaScrollPane;
	}
	
	/**
	 * This function loads all Types from DB to updateTypesIDComboBox
	 */
	public void updateTypesIDComboBox(){
		int i=0,itemcount = getTypesIDComboBox().getItemCount();
		if (itemcount > 0) {
			getTypesIDComboBox().addItem(":");
			getTypesIDComboBox().setSelectedItem(":");				
		}
		for (i=0; i < itemcount ; i++ ){				
			getTypesIDComboBox().removeItemAt(0);						
		}		
		String[] typeList = null;
		try{
			ConnectionToServer connectiontoserver = new ConnectionToServer();		
			typeList = (String[]) connectiontoserver.loadAllCategoryTypes(CategoryID);
			if (typeList == null){
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				getTypesIDComboBox().addItem(":");
			}	
			else{
				int len = typeList.length;
				if (len == 0)
					getTypesIDComboBox().addItem(":");						
				for(i=0; i < len; i++){
					getTypesIDComboBox().addItem(typeList[i]);
				}	
			}
		}
		catch(Exception e){
			JOptionPane.showMessageDialog(null, "ERROR!!!");
			getTypesIDComboBox().addItem(":");
		}
	}
}
